package com.org.gundutubruk;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * intinya sama kayak gamesetting, tapi player yang dipilih cuman bisa satu doang
 * buat ke activity berikunya panggil lanjutKeRoom()
 */

public class SelectServerGameActivity extends Activity {

	SelectServerGameActivity self;
	int gbrApatar = R.drawable.apatar0;
	int gbrApatarKosong = R.drawable.apatar;
	
	int currMap;
	int currPlayer;
	
	Button incGundu;
	Button decGundu;
	TextView jumgundu;
	Button incmode;
	Button decmode;
	TextView mode;
	Button lanjut;
	Button addPlayer;
	Button removePlayer;
	Button kiriMap;
	Button kananMap;
	Button panahbawah;
	Button panahatas;
	ImageView mapnow;
	Button tutor;
	RelativeLayout scrolling;
	boolean atas;
	float awal;
	float first;
	float curry;
	Thread tAtas;
	Thread tBawah;
	Thread blink;
	Context context;
	boolean isRunning;
	int Max;
	int stateBlink;
	int angkamode;
	//variabel yang perlu diisi
	String namaP;
	int jumGundu;//variabel jumlah gundu untuk gundu taruhan(BUKAN PLAYER)
	int jumGunduPlayer;
	int avatar;	
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    	
		setContentView(R.layout.select_server_layout);
		self = this;
		context=this;
		Log.d("", "Nama pemain : "+namaP);
		Log.d("", "Avatar pemain : "+avatar);
		castItem();
		init();
		isiFungsiItem();
		
		Intent i = getIntent();
		namaP = i.getStringExtra("namaPlayer");
		jumGunduPlayer = i.getIntExtra("jumGundu", 0);
		avatar = i.getIntExtra("avatar", 0);
	}

	public void castItem(){
		incGundu=(Button)findViewById(R.id.incrementjumgundu);
		decGundu=(Button)findViewById(R.id.decrementjumgundu);
		jumgundu=(TextView)findViewById(R.id.displayjumgundu);
		incmode=(Button)findViewById(R.id.incrementmode);
		decmode=(Button)findViewById(R.id.decrementmode);
		mode=(TextView)findViewById(R.id.mode);
		lanjut=(Button)findViewById(R.id.next);
		scrolling=(RelativeLayout)findViewById(R.id.layoutdiscroll);
		kananMap=(Button)findViewById(R.id.incrementmap);
		kiriMap=(Button)findViewById(R.id.decrementmap);
		mapnow=(ImageView)findViewById(R.id.displaymap);
		panahatas=(Button)findViewById(R.id.panahatas);
		panahbawah=(Button)findViewById(R.id.panahbawah);
		tutor=(Button)findViewById(R.id.tutorial);
	}
	
	public boolean onTouchEvent(MotionEvent event){
		final int actioncode = event.getAction() & MotionEvent.ACTION_MASK;
			if(actioncode==MotionEvent.ACTION_DOWN){
				awal=event.getY();
				first=event.getY();
			}else if(actioncode==MotionEvent.ACTION_MOVE){
				curry=event.getY();
				scrolling.scrollBy(0, (int)(awal-curry));
				awal=curry;
			}else if(actioncode==MotionEvent.ACTION_UP){
				curry=event.getY();
				if(atas){
					if(first-curry<0||first-curry<100){
						scrolling.scrollTo(0, 0);
					}else if(first-curry>100){
						scrolling.scrollTo(0, 480);
						atas=false;
					}
				}else{
					if(first-curry>0||first-curry>-100){
						scrolling.scrollTo(0, 480);
					}else if(first-curry<-100){
						scrolling.scrollTo(0, 0);
						atas=true;
					}
				}
			}
		return false;
	}
	
	
	public void init(){
		currMap=1;
		jumGundu=1;
		jumgundu.setText(""+jumGundu);
		angkamode=1;
		GameSettingHolder.statusTutorial = false;
		mode.setText("Buletan");
		isRunning=true;
		final Handler h = new Handler();
		blink=new Thread(){
			public void run(){
				while(isRunning){
					h.post(new Runnable(){
						public void run(){
							if(stateBlink==1){
								panahatas.setBackgroundResource(R.drawable.atas2);
								panahbawah.setBackgroundResource(R.drawable.bawah2);
								stateBlink=0;
							}else{
								panahatas.setBackgroundResource(R.drawable.atas);
								panahbawah.setBackgroundResource(R.drawable.bawah);
								stateBlink=1;
							}
						}
					});
					try {
						sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		blink.start();
	}
	
	public void isiFungsiItem(){
		decGundu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(jumGundu>1)
					jumGundu--;
				jumgundu.setText(""+jumGundu);
			}
		});
		incGundu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				jumGundu++;
				jumgundu.setText(""+jumGundu);
			}
		});
		incmode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(angkamode<3)
					angkamode++;
				if(angkamode==1)
					mode.setText("Buletan");
				else if(angkamode==2)
					mode.setText("Apollo");
				else if(angkamode==3)
					mode.setText("Duel");
			}
		});
		decmode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(angkamode>1)
					angkamode--;
				if(angkamode==1)
					mode.setText("Buletan");
				else if(angkamode==2)
					mode.setText("Apollo");
				else if(angkamode==3)
					mode.setText("Duel");
			}
		});
		kiriMap.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currMap>1){
					currMap--;
					mapnow.setImageResource(R.drawable.map1mini);
				}
			}
		});
		kananMap.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currMap<2){
					currMap++;
					mapnow.setImageResource(R.drawable.map2mini);
				}
			}
		});
		panahatas.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.d("","Ini klik "+scrolling.getScrollY());
				final Handler h = new Handler();
				tAtas = new Thread(){
					public void run(){
		                int y = scrolling.getScrollY();
		                while(y>0){
		                    final int Y = y;
		                    h.post(new Runnable() {
		                        public void run() {
		                            scrolling.scrollTo(0, Y);
		                        }
		                    });
		                    y-=2;
		                    panahbawah.setClickable(false);
		                    try {
		                        sleep(100/12);
		                    } catch (InterruptedException e) {
		                    }
		                }
		                panahbawah.setClickable(true);
		            }
				};
				tAtas.start();
			}
		});
		panahbawah.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.d("","Ini klik "+scrolling.getScrollY());
				final Handler h = new Handler();
				tBawah = new Thread(){
					public void run(){
		                int y = scrolling.getScrollY();
		                while(y<480){
		                    final int Y = y;
		                    h.post(new Runnable() {
		                        public void run() {
		                            scrolling.scrollTo(0, Y);
		                        }
		                    });
		                    y+=2;
		                    panahatas.setClickable(false);
		                    try {
		                        sleep(100/12);
		                    } catch (InterruptedException e) {
		                    }
		                }
		                panahatas.setClickable(true);
		            }
				};
				tBawah.start();
			}
		});
		tutor.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(GameSettingHolder.statusTutorial){
					GameSettingHolder.statusTutorial=false;
					tutor.setBackgroundResource(R.drawable.tutorial);
				}else{
					GameSettingHolder.statusTutorial=true;
					tutor.setBackgroundResource(R.drawable.tutorial_selected);
				}
			}
		});
		lanjut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				lanjutKeRoom();
			}
		});
	}

	public void onBackPressed(){	
		overridePendingTransition(R.anim.fade, R.anim.hold);
        this.finish();
	}
	
	public void lanjutKeRoom() {
		setting();
        Intent intent = new Intent(self, RoomMultiPlayer.class);
        intent.putExtra("TYPE", 0);//sebagai server
        intent.putExtra("namaPlayer", namaP);
        intent.putExtra("jumGundu", jumGunduPlayer);
        intent.putExtra("avatar", avatar);
        startActivity(intent);
        overridePendingTransition(R.anim.fade, R.anim.hold);
        this.finish();
	}
	
	public void setting(){
		GameSettingHolder.modegame=angkamode;
		GameSettingHolder.taruhan=jumGundu;
		GameSettingHolder.playMap=currMap;
	}
	
	public static ArrayList<String> validasi(int mode, int taruhan, ArrayList<Player>semua){
		ArrayList<String> retval=new ArrayList<String>();
		if(mode==1){
			if(taruhan*semua.size()>16){
				retval.add("Maksimal gundu yang ditaruhkan adalah 16");
			}
		}else if(mode==2){
			if(taruhan*semua.size()>10){
				retval.add("Maksimal gundu yang ditaruhkan adalah 10");
			}
		}else if(mode==3){
			if(semua.size()>5){
				retval.add("Maksimal jumlah pemain dalam duel hanya 5 orang");
			}
			if(taruhan>1) {
				retval.add("Dalam mode duel hanya dapat mempertaruhkan 1 gundu");
			}
		}
		if(minJumGundu(semua)<taruhan){
			retval.add("Ada pemain yang jumlah gundunya kurang dari taruhan");
		}
		return retval;
	}
	
	private static int minJumGundu(ArrayList<Player>P){
		int min;
		if(P.size()>0){
			min=P.get(0).jumlahGundu;
			for(Player Pl:P){
				if(Pl.jumlahGundu<min)
					min=Pl.jumlahGundu;
			}
			return min;
		}else{
			return -1;
		}
	}
	
}
